package com.example.mapp.buildSql;

import com.example.entity.demandMaintance.DemandMaintanceApply;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class DemandMaintAppSql {

    public String queryDemandMaints(DemandMaintanceApply demandMaintanceApply){
      StringBuilder builder = new StringBuilder(" select * from demand_maintance_apply where 1=1 ");

      if(StringUtils.isNotEmpty(demandMaintanceApply.getUserPartyId())){
          builder.append(" and user_party_id = #{userPartyId}");
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getUserPartyName())){
          String tempName = "'%"+ demandMaintanceApply.getUserPartyName() + "%'";
          builder.append(" and user_party_name like ").append(tempName);
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getMaintenancePartyName())){
          String tempName = "'%"+ demandMaintanceApply.getMaintenancePartyName() + "%'";
          builder.append(" and maintenance_party_name like ").append(tempName);
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getMaintenancePartyId())){
          builder.append(" and maintenance_party_id = #{maintenancePartyId}");
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getMaintenancePartyName())){
          String maintenancePartyName = "'%"+demandMaintanceApply.getMaintenancePartyName()+"%'";
          builder.append(" and maintenance_party_name like ").append(maintenancePartyName);
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getStartDate())){
          builder.append(" and start_date >= #{startDate}");
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getEndDate())){
          builder.append(" and end_date <= #{endDate}");
      }
      //区级监管单位查询
      if(StringUtils.isNotEmpty(demandMaintanceApply.getParentPartyId())){
          builder.append(" and parent_party_id = #{parentPartyId}");
          if("1".equals(demandMaintanceApply.getApplyStatus())){
              builder.append(" and apply_status not in ('0')");
          }
      }
      if(StringUtils.isNotEmpty(demandMaintanceApply.getParentParentPartyId())){
          builder.append(" and parent_parent_party_id = #{parentParentPartyId}");
          if("2".equals(demandMaintanceApply.getApplyStatus())){
              builder.append(" and apply_status not in ('0','1')");
          }
      }


      return builder.toString();
    }

    /**
     * 修改按需维保申请
     * @param map
     * @return
     */
    public String updateDemandMaint(Map map){
        StringBuilder builder = new StringBuilder(" update demand_maintance_apply set ");
        StringBuilder setValue = new StringBuilder();
        if(map.get("applyStatus") != null && !"".equals(map.get("applyStatus"))){
            setValue.append(" apply_status = #{applyStatus},");
        }
        if(map.get("maintenancePartyId") != null && !"".equals(map.get("maintenancePartyId"))){
            setValue.append(" maintenance_party_id = #{maintenancePartyId},");
        }
        if(map.get("maintenancePartyName") != null && !"".equals(map.get("maintenancePartyName"))){
            setValue.append(" maintenance_party_name = #{maintenancePartyName},");
        }
        if(map.get("haveTestSystem") != null && !"".equals(map.get("haveTestSystem"))){
            setValue.append(" have_test_system = #{haveTestSystem},");
        }
        if(map.get("haveInsurance") != null && !"".equals(map.get("haveInsurance"))){
            setValue.append(" have_insurance = #{haveInsurance},");
        }
        if(map.get("maintenanceCycle") != null && !"".equals(map.get("maintenanceCycle"))){
            setValue.append(" maintenance_cycle = #{maintenanceCycle},");
        }

        if(map.get("maintenceServiceStatementDoc") != null && !"".equals(map.get("maintenceServiceStatementDoc"))){
            setValue.append(" maintence_service_statement_doc = #{maintenceServiceStatementDoc},");
        }
        if(map.get("letterOfCommitmentDoc") != null && !"".equals(map.get("letterOfCommitmentDoc"))){
            setValue.append(" letter_of_commitment_doc = #{letterOfCommitmentDoc},");
        }
        if(map.get("elevatorIds") != null && !"".equals(map.get("elevatorIds"))){
            setValue.append(" elevator_ids = #{elevatorIds},");
        }
        if(map.get("insuranceName") != null && !"".equals(map.get("insuranceName"))){
            setValue.append(" insurance_name = #{insuranceName},");
        }
        if(map.get("insuranceDate") != null && !"".equals(map.get("insuranceDate"))){
            setValue.append(" insurance_date = #{insuranceDate},");
        }
        if(map.get("remarks") != null && !"".equals(map.get("remarks"))){
            setValue.append(" remarks = #{remarks},");
        }

        builder.append(setValue.substring(0,setValue.lastIndexOf(","))).append(" where 1=1 ");
        if(map.get("id") != null && !"".equals(map.get("id"))){
            builder.append(" and id = #{id}");
        }

        return builder.toString();
    }
}
